home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-06-14 | 9.1 KB | 289 lines | [TEXT/MPS ] |
- // DylanTalk by Dean Yu & Fred Monroe (CEO & Chief Technology Officer)
- //
- // © 1992 FGM Enterprises. All rights reserved.
- //
- // Personality.c – Code that handles preferences and switching personality files
- //
-
- #include <Types.h>
- #include <Errors.h>
- #include <Files.h>
- #include <Folders.h>
- #include <Fonts.h>
- #include <GestaltEqu.h>
- #include <Layers.h>
- #include <Menus.h>
- #include <OSUtils.h>
- #include <Resources.h>
- #include <SysEqu.h>
- #include <ToolUtils.h>
- #include <Windows.h>
-
- #include <DylanTalk.h>
-
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // Routine to open the preferred personality file, or the default one if the preferred
- // one isn’t there. Also creates the preferences file & folder if they don’t exist.
- pascal void OpenPersonalityFile(void)
- {
- OSErr err;
- short prefsVRefNum;
- short prefsRefNum;
- short personalityRefNum;
- long prefsDirID;
- DylanTalkGlobalHandle globals;
- StringHandle prefsInfo;
- CInfoPBRec catInfo;
- HParamBlockRec folderInfo;
-
- Gestalt(gestaltDylanTalk, &((long) globals));
- if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &prefsVRefNum, &prefsDirID) == noErr) {
- prefsInfo = GetResource('STR ', rDylanTalkPreferencesFile);
- DetachResource(prefsInfo);
- (**globals).preferencesFileName = prefsInfo;
-
- // See if our preferences folder is there.
-
- catInfo.dirInfo.ioNamePtr = *prefsInfo;
- catInfo.dirInfo.ioVRefNum = prefsVRefNum;
- catInfo.dirInfo.ioFDirIndex = 0;
- catInfo.dirInfo.ioDrDirID = prefsDirID;
- err = PBGetCatInfoSync(&catInfo);
-
- // If it’s not, create it.
-
- if (err == fnfErr) {
- folderInfo.fileParam.ioNamePtr = *prefsInfo;
- folderInfo.fileParam.ioVRefNum = prefsVRefNum;
- folderInfo.fileParam.ioDirID = prefsDirID;
- err = PBDirCreateSync(&folderInfo); // Create DylanTalk Preferences folder
- if (err == noErr)
- prefsDirID = folderInfo.fileParam.ioDirID;
- }
- else
- prefsDirID = catInfo.dirInfo.ioDrDirID;
-
- // Save the directory in our globals
-
- (**globals).preferencesVRefNum = prefsVRefNum;
- (**globals).preferencesDirID = prefsDirID;
-
- // Try to create the preferences file inside the folder. (If it already exists, we’ll just get an error back.
-
- HCreateResFile(prefsVRefNum, prefsDirID, *prefsInfo);
- prefsRefNum = HOpenResFile(prefsVRefNum, prefsDirID, *prefsInfo, fsCurPerm); // Open the preferences file.
-
- prefsInfo = Get1Resource('STR ', rPreferredPersonality); // Get the preferred personality file
- if (prefsInfo == nil) {
- prefsInfo = GetResource('STR ', rDefaultPersonality); // Get the default one if there is no preferred one.
- DetachResource((Handle) prefsInfo); // Detach the default string
- AddResource((Handle) prefsInfo, 'STR ', rPreferredPersonality, "\p"); // And use it as the preferred personality.
- WriteResource((Handle) prefsInfo);
- }
-
- // Now open the personality file
-
- personalityRefNum = SwitchPersonalityFile(*prefsInfo); // Switch to it.
-
- // Save the personality file in our globals
-
- (**globals).personalityFile = personalityRefNum;
-
- // Close the preferences file
-
- CloseResFile(prefsRefNum);
- }
- }
-
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // ChooseNewPersonality – Close the current personality file, and open the one
- // that the user just picked from the menu
- pascal void ChooseNewPersonality(short itemNumber, MenuHandle personalityMenu)
- {
- short currentMark;
- short newPersonalityFile;
- short prefsRefNum;
- DylanTalkGlobalHandle globals;
- Handle oldPersonality;
- Str255 newPersonality;
-
- // If the user picked the current personality, don’t bother doing anything
-
- GetItemMark(personalityMenu, itemNumber, ¤tMark);
- if (currentMark != checkMark) {
- Gestalt(gestaltDylanTalk,&((long) globals));
-
- // Switch personality files
-
- GetItem(personalityMenu, itemNumber, &newPersonality);
- newPersonalityFile = SwitchPersonalityFile(newPersonality);
- (**globals).personalityFile = newPersonalityFile;
-
- // Uncheck the old personality, and check the new one
-
- CheckItem(personalityMenu, (**globals).personalityItem, false);
- CheckItem(personalityMenu, itemNumber, true);
- (**globals).personalityItem = itemNumber;
-
- // Write the new personality out to the preferences file
-
- prefsRefNum = HOpenResFile((**globals).preferencesVRefNum, (**globals).preferencesDirID,
- *((**globals).preferencesFileName), fsCurPerm);
- oldPersonality = Get1Resource('STR ', rPreferredPersonality);
- if (oldPersonality == nil) {
- oldPersonality = (Handle) NewString(newPersonality);
- AddResource(oldPersonality, 'STR ', rPreferredPersonality, "\p");
- }
- else {
- PtrToXHand(newPersonality, oldPersonality, (long) (newPersonality[0] + 1));
- ChangedResource(oldPersonality);
- }
- WriteResource(oldPersonality);
- CloseResFile(prefsRefNum);
- }
- }
-
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- pascal short SwitchPersonalityFile(Str255 personalityFile)
- {
- short refNum;
- short resRef;
- short prefsVRefNum;
- long prefsDirID;
- res_map_handle map_copy;
- res_map_handle curr_map;
- DylanTalkGlobalHandle globals;
-
- // Get our globals
-
- Gestalt(gestaltDylanTalk, &((long) globals));
- prefsDirID = (**globals).preferencesDirID;
- prefsVRefNum = (**globals).preferencesVRefNum;
-
- // Close the current personality file if one is open
-
- if ((refNum = (**globals).personalityFile) > 0)
- CloseResFile(refNum);
-
- // Open up the new one.
-
- refNum = HOpenResFile(prefsVRefNum, prefsDirID, personalityFile, fsCurPerm);
- if (!ResError())
- {
- map_copy = *(res_map_handle *)TopMapHndl; // Get handle to Personality resource map
-
- curr_map = (*map_copy)->next_map; // Get the map underneath the Personality file
- resRef = (*curr_map)->refNum; // And it’s file reference number
-
- *((res_map_handle *)TopMapHndl) = curr_map; // Make that map the top and current map
- *((short *)CurMap) = resRef;
-
- (*map_copy)->next_map = nil; // Put Personality file at the bottom of the resource chain
- while ((*curr_map)->next_map != nil) // Look for the current bottom of the chain
- curr_map = (*curr_map)->next_map;
- (*curr_map)->next_map = map_copy; // And tack the Personality file after that.
- }
- else
- refNum = 0;
-
- return refNum;
- }
-
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // Routine to look for personality files in the Preferences folder, and put each
- // one it finds in the personality menu.
-
- pascal void FillPersonalityMenu(MenuHandle personalityMenu)
- {
- short fileIndex;
- short personalityCount;
- short prefsVRefNum;
- OSErr err;
- long prefsDirID;
- Str255 prefsFileName;
- CInfoPBRec catInfo;
- DylanTalkGlobalHandle globals;
-
- // Install the about item and a line at the top of the menu
-
- AppendMenu(personalityMenu, "\pAbout DylanTalk…");
- AppendMenu(personalityMenu, "\p(-");
-
- // Get our DylanTalk globals
-
- Gestalt(gestaltDylanTalk, &((long) globals));
- prefsDirID = (**globals).preferencesDirID;
- prefsVRefNum = (**globals).preferencesVRefNum;
-
- // If our About Box window didn’t load, or Times 72 isn’t availabe, disable
- // the About Box item.
-
- if ((**globals).aboutPicture == nil)
- DisableItem(personalityMenu, 1);
-
- fileIndex = 1;
- personalityCount = 3;
-
- // Set up some stuff for GetCatInfo
-
- catInfo.hFileInfo.ioCompletion = nil;
- catInfo.hFileInfo.ioNamePtr = prefsFileName;
- catInfo.hFileInfo.ioVRefNum = prefsVRefNum;
-
- do {
- catInfo.hFileInfo.ioFDirIndex = fileIndex++;
- catInfo.hFileInfo.ioDirID = prefsDirID;
- err = PBGetCatInfoSync(&catInfo); // Get file information
-
- // If this file is a Personality file, add it to the Personality menu
-
- if (err == noErr &&
- ((catInfo.hFileInfo.ioFlFndrInfo.fdType == kDylanTalkPersonalityFileType) &&
- (catInfo.hFileInfo.ioFlFndrInfo.fdCreator == kDylanTalkCreator))) {
- AppendMenu(personalityMenu, "\p ");
- SetItem(personalityMenu, personalityCount++, prefsFileName);
-
- // Check the current personality.
-
- if (catInfo.hFileInfo.ioFRefNum == (**globals).personalityFile) {
- CheckItem(personalityMenu, personalityCount - 1, true);
- (**globals).personalityItem = personalityCount - 1;
- }
- }
- } while (err == noErr);
- }
-
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // DylanTalk About Box
-
- pascal void DylanTalkAbout(void)
- {
- DylanTalkGlobalHandle globals;
- WindowPtr aboutBox;
- PicHandle aboutBoxPicture;
- GrafPtr oldPort;
- Rect boundsRect;
- WindowRecord aboutBoxStorage;
-
- Gestalt(gestaltDylanTalk, &((long) globals)); // Get our globals
- aboutBoxPicture = (**globals).aboutPicture;
-
- SetRect(&boundsRect, ((**aboutBoxPicture).picFrame).left, ((**aboutBoxPicture).picFrame).top,
- ((**aboutBoxPicture).picFrame).right, ((**aboutBoxPicture).picFrame).bottom);
- aboutBox = NewWindow(&aboutBoxStorage, &boundsRect, "\p", false, altDBoxProc, (WindowPtr) -1, false, 0);
- AutoPositionWindow(aboutBox, lcMainScreen, hcCenter, vcAlertCenter);
-
- ShowWindow(aboutBox); // Show it.
- GetPort(&oldPort);
- SetPort(aboutBox);
- DrawPicture(aboutBoxPicture, &boundsRect);
- SetPort(oldPort);
-
- // Wait for the user to click the mouse
-
- do {
- } while (Button() == false);
-
- CloseWindow(aboutBox);
- }
-